﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace CustSign
{
    public partial class frmCustSign : Form
    {
        String strWelComeWord;

        public frmCustSign()
        {
            InitializeComponent();
        }

        private void frmCustSign_Load(object sender, EventArgs e)
        {
            FontBase FontNow = new FontBase();

            this.Top = 0;
            this.Left = 0;
            this.Width = Screen.GetWorkingArea(this).Width;
            this.Height = Screen.GetWorkingArea(this).Height;

            this.BackgroundImage = Properties.Resources.imageback;

            lblInfo.Text = "";
            lblInfo.BackColor = Color.Transparent;
            lblInfo.Left = 0;
            lblInfo.Top = 0;
            lblInfo.Width = this.Width;
            lblInfo.Height = this.Height;
            
            FontNow.FontGet();
            lblInfo.ForeColor = FontNow.cColor;
            lblInfo.Font = FontNow.fFont;
            strWelComeWord = FontNow.strWelcome;

            txtRead.Focus();
        }

        private void txtRead_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                CustSignTrue(txtRead.Text);
                txtRead.Text = "";
            }
            if (e.KeyChar == 27)
            {
                this.Dispose();
            }
        }

        private void txtRead_Leave(object sender, EventArgs e)
        {
            txtRead.Focus();
        }

        private void CustSignTrue(String strRead)
        {
            String strPan,strWelComeNow;

            strPan = strRead.Substring(1, 16);

            OleDbConnection Conn = new OleDbConnection(Properties.Settings.Default.strOleConn);

            OleDbCommand Comm = Conn.CreateCommand();
            Comm.CommandText = "SELECT a.Pan, a.CustName, b.TypeName AS Sex, a.CompName, a.Duty, a.Phone FROM (TCustList a LEFT OUTER JOIN (SELECT TypeValue, TypeName FROM TDictionary WHERE (TypeField = 'Sex')) b ON a.Sex = b.TypeValue) where Pan = '" + strPan + "'";

            Conn.Open();
            OleDbDataReader CustRead = Comm.ExecuteReader();

            if (CustRead.HasRows)
            {
                while (CustRead.Read())
                {
                    strWelComeNow = strWelComeWord;
                    //strWelComeNow = strWelComeNow.Replace("[卡号]", (String)CustRead["Pan"]);
                    //strWelComeNow = strWelComeNow.Replace("[姓名]", (String)CustRead["CustName"]);
                    //strWelComeNow = strWelComeNow.Replace("[性别]", (String)CustRead["Sex"]);
                    //strWelComeNow = strWelComeNow.Replace("[公司]", (String)CustRead["CompName"]);
                    //strWelComeNow = strWelComeNow.Replace("[职务]", (String)CustRead["Duty"]);
                    //strWelComeNow = strWelComeNow.Replace("[电话]", (String)CustRead["Phone"]);

                    strWelComeNow = WelComeCHG(strWelComeNow, "[卡号]", CustRead, "Pan");
                    strWelComeNow = WelComeCHG(strWelComeNow, "[姓名]", CustRead, "CustName");
                    strWelComeNow = WelComeCHG(strWelComeNow, "[性别]", CustRead, "Sex");
                    strWelComeNow = WelComeCHG(strWelComeNow, "[公司]", CustRead, "CompName");
                    strWelComeNow = WelComeCHG(strWelComeNow, "[职务]", CustRead, "Duty");
                    strWelComeNow = WelComeCHG(strWelComeNow, "[电话]", CustRead, "Phone");

                    lblInfo.Text = strWelComeNow;
                }
            }
            else
            {
                lblInfo.Text = "欢迎光临";
            }
            CustRead.Close();

            Comm.CommandText = "insert into TSigns (EventID,Pan,Sign) values(1,'" + strPan + "',1)";
            Comm.ExecuteNonQuery();
            
            CustRead.Dispose();
            Comm.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        private String WelComeCHG(String strSource,String strObject, OleDbDataReader rdSource, String strFiled)
        {
            if (DBNull.Value.Equals(rdSource[strFiled]))
            {
                return strSource.Replace(strObject, "");
            }
            else
            {
                return strSource.Replace(strObject, (String)rdSource[strFiled]);
            }
        }
    }
}
